package iosr11.gbase.dao;

import iosr11.data.ProjectGroup;
import iosr11.data.ProjectSubject;
import iosr11.data.Student;

import java.util.logging.Logger;

import com.google.api.gbase.client.FeedURLFactory;
import com.google.api.gbase.client.GoogleBaseEntry;
import com.google.api.gbase.client.GoogleBaseFeed;
import com.google.api.gbase.client.GoogleBaseQuery;
import com.google.gdata.data.PlainTextConstruct;

/*
 * nie testowane draft
 */

public class ProjectGroupDAOImpl extends GenericDAOImpl<ProjectGroup> implements ProjectGroupDAO{

	private static String TYPE = "project group";
	private static String NAME = "name";
	private static String SUBJECT_REF = "subject";
	private Logger log = Logger.getLogger("service-log");
	
	@Override
	public ProjectGroup getByName(String name) {
		GoogleBaseQuery query = new GoogleBaseQuery(FeedURLFactory.getDefault().getItemsFeedURL());
		GoogleBaseFeed feed = DAOLocatorImpl.getInstance().getGoogleBaseQueryResult(query, NAME, name);
		if (feed==null){
			log.info("brak rekordow w bazie o polu name");
			return null;
		}
		feed = DAOLocatorImpl.getInstance().selectByType(feed, TYPE);
		
		if (feed == null) {
			log.warning("brak grupy o podanej nazwie");
			return null;
		}
		if (feed.getEntries().size()!=1) {
			log.warning("zla ilosc rekordow odnoszacych sie do jednej nazwy (rozna od 1)");
			return null;
		}
		
		ProjectGroup result = this.rewrite(feed.getEntries().get(0));
		return result;
	}

	@Override
	public ProjectGroup getStudentsGroup(Student student) {
		// TODO Auto-generated method stub - nie to DAO
		return null;
	}

	@Override
	public boolean checkType(GoogleBaseEntry entry) {
		return entry.getGoogleBaseAttributes().getItemType().equals(TYPE);
	}

	@Override
	public ProjectGroup rewrite(GoogleBaseEntry entry) {
		ProjectGroup group = new ProjectGroup();
		group.setId(entry.getId());
		group.setStudents(null);
		group.setGroupName(entry.getGoogleBaseAttributes().getTextAttribute(NAME));
		if (!entry.getGoogleBaseAttributes().getTextAttribute(SUBJECT_REF).contains("http")){
			if (!entry.getGoogleBaseAttributes().getTextAttribute(SUBJECT_REF).contains("null")){
				log.info("zly rekord");
				return null;
			}
			else {
				log.info("brak tematu do grupy");
				group.setSubject(null);
				return group;
			}
		}
		ProjectSubject subject = new ProjectSubject();
		subject.setId(entry.getGoogleBaseAttributes().getTextAttribute(SUBJECT_REF));
		group.setSubject(subject);
		return group;
	}

	@Override
	public GoogleBaseEntry rewrite(ProjectGroup entity) {
		GoogleBaseEntry entry = new GoogleBaseEntry();
		entry.setDraft(true);
		entry.getGoogleBaseAttributes().setItemType(TYPE);
		entry.setTitle(new PlainTextConstruct(TYPE));
		entry.getGoogleBaseAttributes().addTextAttribute(NAME, entity.getGroupName());
		if (entity.getSubject()!=null)
			if (entity.getSubject().getId()!=null){
				entry.getGoogleBaseAttributes().addTextAttribute(SUBJECT_REF, entity.getSubject().getId());
				return entry;
			}
		entry.getGoogleBaseAttributes().addTextAttribute(SUBJECT_REF, "null");
		return entry;
	}
	
}
